﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DunLibrary.Dun;
using DunSilverlight.Comm;
using Telerik.Windows.Controls;

namespace DunSilverlight.Views
{
    public partial class ThridInvForm : UserControl
    {
        private InvestigateRequest req;

        public Action<InvestigateRequest> SaveCallBack;

        public ThridInvForm(InvestigateRequest req)
        {
            this.req = req;
            
            InitializeComponent();
            Loaded += new RoutedEventHandler(ThridInvForm_Loaded);
        }

        void ThridInvForm_Loaded(object sender, RoutedEventArgs e)
        {
            DataContext = req;
            
            LoadCheckers();//读取本部门有审核权的人员列表
            LoadInvType();
        }

        /// <summary>
        /// 读取本部门有审核权限的人
        /// </summary>
        private void LoadCheckers()
        {
            UserService.UserServiceClient ser = new UserService.UserServiceClient();

            ser.GetListByHQLCompleted += (object sender, UserService.GetListByHQLCompletedEventArgs e) =>
                {
                    int totalCount = 0;
                    IList<DunLibrary.User.User> users = Comm.JsonSerializerHelper.JsonToEntities<DunLibrary.User.User>(e.Result, out totalCount);
                    cmbChecker.ItemsSource = users;
                };
            
            string hql = string.Format("select u from User as u inner join u.Role as r left join r.Rights as t where u.Organization.Id={0} and t.Number='{1}'", AppGlobal.CurrentUser.Organization.Id, Comm.RightManagement.Right_Investigate_Check);
            ser.GetListByHQLAsync(hql);
        }

        /// <summary>
        /// 数据导入
        /// </summary>
        private void LoadInvType()
        {
            Action<IList<DunLibrary.Base.InvestigationType>> callBack=(IList<DunLibrary.Base.InvestigationType> types) =>
            {
                cmbInvType.ItemsSource = types;
            };
            AppGlobal.GetInvTypes(callBack);
        }

        private bool CheckVerify()
        {
            if (req.Checker == null)
            {
                Tools.ShowMessage("必须选择审核人!", "", false);
                return false;
            }

            if (req.InvType == null)
            {
                Tools.ShowMessage("必须选择调查类型!", "", false);
                return false;
            }

            if (string.IsNullOrEmpty(req.RequestContent))
            {
                Tools.ShowMessage("请录入申请理由!", "", false);
                return false;
            }

            return true;
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            (this.Parent as RadWindow).Close();
        }
        /// <summary>
        /// 保存一个调查申请
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (CheckVerify())
            {
                InvRequestService.InvestigateRequestServiceClient ser = new InvRequestService.InvestigateRequestServiceClient();

                ser.SaveOrUpdateCompleted += (object sender1, InvRequestService.SaveOrUpdateCompletedEventArgs e1) =>
                    {
                        int id = 0;

                        id = Comm.JsonSerializerHelper.JsonToEntity<int>(e1.Result);


                        if (id > 0)
                        {
                            req.Id = id;
                            Tools.ShowMessage("保存成功!", "", true);
                            if(SaveCallBack!=null)
                                SaveCallBack(req);
                        }
                    };

                string json = Newtonsoft.Json.JsonConvert.SerializeObject(req);
                ser.SaveOrUpdateAsync(json);
            }
        }
    }
}
